//
//  SpecialDetailsViewController.m
//  悠狸
//
//  Created by dlios on 15-8-30.
//  Copyright (c) 2015年 Dung. All rights reserved.
//

#import "SpecialDetailsViewController.h"
#import "MyModel.h"
#import "Webview.h"
#import "GiftTableViewCell.h"
#import "Asyn.h"
#import "URL.h"
#import "Tools.h"
#import "CateModel.h"

@interface SpecialDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UITableView *table;


@end

@implementation SpecialDetailsViewController


- (void)dealloc
{
    
    
    [_model release];
    [_array release];
    [_table release];
    [_catearray release];
    [_catemodel release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self= [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.catearray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.model.title;
    
    
    
    self.navigationController.navigationBar.translucent = NO;
    [self create];
    if (_catearray.count > 0) {
        NSString *str = [NSString stringWithFormat:@"%@",self.catemodel.Id];
        self.title = self.catemodel.title;

        [self parser:str];
    }if (_catearray.count == 0) {
        [self parser:self.model.Id];
    }
    
  
}
//http://api.liwushuo.com/v2/collections/109/posts?limit=20&offset=0
- (void)parser:(NSString *)myid
{
    
    NSString *url = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%@/posts?limit=20&offset=0", myid];
    [Asyn startParser:url partemer:nil file:@"data10" block:^(NSData *data) {
        //存储
        [Tools toolsOfSaveLibraryPathName:@"data10" data:data];
        NSDictionary *dic = [Tools toolsOfJsonDataType:dicType fileName:@"data10"];

        NSDictionary *dic1 = [dic objectForKey:@"data"];
        NSArray *arr = [dic1 objectForKey:@"posts"];
        for (NSDictionary *dic2 in arr) {
            MyModel *model = [[[MyModel alloc] init] autorelease];
            [model setValuesForKeysWithDictionary:dic2];
            [_array addObject:model];
            
        }
        [_table reloadData];
    }];
}
- (void)create
{
    self.table = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain] autorelease];
    _table.rowHeight = 200;
    _table.delegate = self;
    _table.dataSource = self;
    
    [self.view addSubview:_table];
    
    [_table registerClass:[GiftTableViewCell class] forCellReuseIdentifier:@"i"];
//    [_table registerClass:[GiftTableViewCell class] forCellReuseIdentifier:@"z"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    if (self.array.count > 0) {
//           return _array.count;
//    } else{
//        return _catearray.count;
//    }
    return self.array.count;
 
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GiftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"i"];

    if (self.array.count != 0) {
        
        MyModel *model = [_array objectAtIndex:indexPath.row];
        self.model = model;
        [cell setmodel:model];
     
//    } else{
//        GiftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"z"];
//        
//        CateModel *model = [_catearray objectAtIndex:indexPath.row];
//        self.catemodel = model;
//        [cell setCate:model];
//        return cell;
    }
       return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    Webview *web = [[[Webview alloc] init] autorelease];
    
    MyModel *model = [_array objectAtIndex:indexPath.row];
    
     web.model = model;
    [self.navigationController pushViewController:web animated:YES];
    
    
}

@end
